#pragma once

#include <AP_HAL/AP_HAL_Boards.h>
#include <AP_ExternalAHRS/AP_ExternalAHRS_config.h>
#include <AP_MSP/msp.h>
#include <AP_AHRS/AP_AHRS_config.h>
#include <AP_GPS/AP_GPS_config.h>
#include <AP_InertialSensor/AP_InertialSensor_config.h>

#ifndef AP_COMPASS_ENABLED
#define AP_COMPASS_ENABLED 1
#endif

#ifndef AP_COMPASS_DIAGONALS_ENABLED
#define AP_COMPASS_DIAGONALS_ENABLED 1
#endif

#ifndef COMPASS_CAL_ENABLED
#define COMPASS_CAL_ENABLED AP_COMPASS_ENABLED && AP_AHRS_DCM_ENABLED
#endif

#ifndef AP_COMPASS_CALIBRATION_FIXED_YAW_ENABLED
#define AP_COMPASS_CALIBRATION_FIXED_YAW_ENABLED AP_COMPASS_ENABLED && AP_GPS_ENABLED && AP_AHRS_ENABLED
#endif

#define COMPASS_MAX_SCALE_FACTOR 1.5
#define COMPASS_MIN_SCALE_FACTOR (1.0/COMPASS_MAX_SCALE_FACTOR)

// Backend support
#ifndef AP_COMPASS_BACKEND_DEFAULT_ENABLED
#define AP_COMPASS_BACKEND_DEFAULT_ENABLED AP_COMPASS_ENABLED
#endif

#ifndef AP_COMPASS_EXTERNALAHRS_ENABLED
#define AP_COMPASS_EXTERNALAHRS_ENABLED (AP_COMPASS_BACKEND_DEFAULT_ENABLED && AP_EXTERNAL_AHRS_ENABLED)
#endif

#ifndef AP_COMPASS_MSP_ENABLED
#define AP_COMPASS_MSP_ENABLED (AP_COMPASS_BACKEND_DEFAULT_ENABLED && HAL_MSP_SENSORS_ENABLED)
#endif

#ifndef AP_COMPASS_SITL_ENABLED
#define AP_COMPASS_SITL_ENABLED (AP_COMPASS_BACKEND_DEFAULT_ENABLED && AP_SIM_ENABLED)
#endif

#ifndef AP_COMPASS_DRONECAN_ENABLED
#define AP_COMPASS_DRONECAN_ENABLED (AP_COMPASS_BACKEND_DEFAULT_ENABLED && HAL_ENABLE_DRONECAN_DRIVERS)
#endif

#ifndef AP_COMPASS_DRONECAN_HIRES_ENABLED
#define AP_COMPASS_DRONECAN_HIRES_ENABLED 0
#endif

// i2c-based compasses:
#ifndef AP_COMPASS_I2C_BACKEND_DEFAULT_ENABLED
#define AP_COMPASS_I2C_BACKEND_DEFAULT_ENABLED AP_COMPASS_BACKEND_DEFAULT_ENABLED
#endif

#ifndef AP_COMPASS_AK09916_ENABLED
#define AP_COMPASS_AK09916_ENABLED AP_COMPASS_I2C_BACKEND_DEFAULT_ENABLED
#endif

#ifndef AP_COMPASS_AK8963_ENABLED
#define AP_COMPASS_AK8963_ENABLED AP_COMPASS_I2C_BACKEND_DEFAULT_ENABLED
#endif

#ifndef AP_COMPASS_BMM150_ENABLED
#define AP_COMPASS_BMM150_ENABLED AP_COMPASS_I2C_BACKEND_DEFAULT_ENABLED
#endif

#ifndef AP_COMPASS_BMM350_ENABLED
#define AP_COMPASS_BMM350_ENABLED AP_COMPASS_I2C_BACKEND_DEFAULT_ENABLED
#endif

#ifndef AP_COMPASS_HMC5843_ENABLED
#define AP_COMPASS_HMC5843_ENABLED AP_COMPASS_I2C_BACKEND_DEFAULT_ENABLED
#endif

#ifndef AP_COMPASS_ICM20948_ENABLED
#define AP_COMPASS_ICM20948_ENABLED AP_COMPASS_I2C_BACKEND_DEFAULT_ENABLED && AP_INERTIALSENSOR_ENABLED
#endif

#ifndef AP_COMPASS_IIS2MDC_ENABLED
#define AP_COMPASS_IIS2MDC_ENABLED 0
#endif

#ifndef AP_COMPASS_IST8308_ENABLED
#define AP_COMPASS_IST8308_ENABLED AP_COMPASS_I2C_BACKEND_DEFAULT_ENABLED
#endif

#ifndef AP_COMPASS_IST8310_ENABLED
#define AP_COMPASS_IST8310_ENABLED AP_COMPASS_I2C_BACKEND_DEFAULT_ENABLED
#endif

#ifndef AP_COMPASS_LIS3MDL_ENABLED
#define AP_COMPASS_LIS3MDL_ENABLED AP_COMPASS_I2C_BACKEND_DEFAULT_ENABLED
#endif

#ifndef AP_COMPASS_LIS2MDL_ENABLED
#define AP_COMPASS_LIS2MDL_ENABLED AP_COMPASS_I2C_BACKEND_DEFAULT_ENABLED && (HAL_PROGRAM_SIZE_LIMIT_KB > 1024)
#endif

#ifndef AP_COMPASS_LSM303D_ENABLED
#define AP_COMPASS_LSM303D_ENABLED AP_COMPASS_I2C_BACKEND_DEFAULT_ENABLED
#endif

#ifndef AP_COMPASS_LSM9DS1_ENABLED
#define AP_COMPASS_LSM9DS1_ENABLED AP_COMPASS_I2C_BACKEND_DEFAULT_ENABLED
#endif

#ifndef AP_COMPASS_MAG3110_ENABLED
#define AP_COMPASS_MAG3110_ENABLED AP_COMPASS_I2C_BACKEND_DEFAULT_ENABLED
#endif

#ifndef AP_COMPASS_MMC3416_ENABLED
#define AP_COMPASS_MMC3416_ENABLED AP_COMPASS_I2C_BACKEND_DEFAULT_ENABLED
#endif

#ifndef AP_COMPASS_MMC5XX3_ENABLED
#define AP_COMPASS_MMC5XX3_ENABLED AP_COMPASS_I2C_BACKEND_DEFAULT_ENABLED && (HAL_PROGRAM_SIZE_LIMIT_KB > 1024)
#endif

#ifndef AP_COMPASS_QMC5883P_ENABLED
#define AP_COMPASS_QMC5883P_ENABLED AP_COMPASS_I2C_BACKEND_DEFAULT_ENABLED && (HAL_PROGRAM_SIZE_LIMIT_KB > 1024)
#endif

#ifndef AP_COMPASS_QMC5883L_ENABLED
#define AP_COMPASS_QMC5883L_ENABLED AP_COMPASS_I2C_BACKEND_DEFAULT_ENABLED
#endif

#ifndef AP_COMPASS_RM3100_ENABLED
#define AP_COMPASS_RM3100_ENABLED AP_COMPASS_I2C_BACKEND_DEFAULT_ENABLED
#endif

#ifndef AP_COMPASS_PROBING_ENABLED
// FIXME: vast majority of boards define this to 1!
#define AP_COMPASS_PROBING_ENABLED 0
#endif

// boards can specify that they do not want their internal buses
// probed for compasses - we default to probing them if we are probing
// any buses:
#ifndef AP_COMPASS_INTERNAL_BUS_PROBING_ENABLED
#define AP_COMPASS_INTERNAL_BUS_PROBING_ENABLED (AP_COMPASS_PROBING_ENABLED || AP_FEATURE_BOARD_DETECT)
#endif

// some boards do not want to probe the internal buses for IS8310 but
// *do* want to probe internal buses for other compasses:
#ifndef AP_COMPASS_IST8310_INTERNAL_BUS_PROBING_ENABLED
#define AP_COMPASS_IST8310_INTERNAL_BUS_PROBING_ENABLED (AP_COMPASS_IST8310_ENABLED && AP_COMPASS_INTERNAL_BUS_PROBING_ENABLED)
#endif

#ifndef AP_COMPASS_LIS2MDL_EXTERNAL_BUS_PROBING_ENABLED
#define AP_COMPASS_LIS2MDL_EXTERNAL_BUS_PROBING_ENABLED (AP_COMPASS_LIS2MDL_ENABLED && AP_COMPASS_PROBING_ENABLED)
#endif
